package org.lib093.ndkapplication

import android.app.Activity
import android.content.Context
import android.widget.Toast
import org.fmod.FMOD

class VoiceManager private constructor(var context: Context){
    lateinit var path:String
    lateinit var listener:PlayListener
    companion object{
        const val MODE_NORMAL = 0
        const val MODE_LUOLI = 1
        const val MODE_DASHU = 2
        const val MODE_JINGSONG = 3
        const val MODE_KONGLING = 4
        const val MODE_GAOGUAI = 5
        @Volatile private var instance: VoiceManager? = null
        fun getInstance(context: Context) =
            instance ?: synchronized(this) {
                instance ?: VoiceManager(context).also { instance = it }
            }
        init {
            System.loadLibrary("ndkapplication")
        }
    }
    fun setPath(path:String):VoiceManager{
        this.path = path
        return this
    }
    fun init():VoiceManager{
        if (!FMOD.checkInit()){
            FMOD.init(context)
        }
        return this
    }
    fun close(){
        FMOD.close()
        instance = null
    }
    fun onStart(){
        if (::listener.isInitialized)
            listener.start()
    }
    fun onFinish(){
        if (::listener.isInitialized)
            (context as Activity).runOnUiThread(Runnable {
                listener.finish()
            })

    }
    fun show(st: String?) {
        Toast.makeText(App.innstance, st, Toast.LENGTH_LONG).show()
    }
    external fun voiceChangeNative(modeNormal:Int,path:String)
    external fun getPlayStateNative():Boolean


    interface PlayListener{
        fun start();
        fun finish();
    }

}